/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.project.carshop07.ctrl.servlets;


import edu.chl.project.carshop07.constants.Actions;
import edu.chl.project.carshop07.constants.Attributes;
import edu.chl.project.carshop07.constants.Pages;
import edu.chl.project.carshop07.constants.Patterns;
import edu.chl.project.carshop07.constants.Requests;
import edu.chl.project.carshop07.core.CarList;
import edu.chl.project.carshop07.core.Customer;
import edu.chl.project.carshop07.core.ShoppingCart;
import edu.chl.project.carshop07.db.Database;
import edu.chl.project.carshop07.xml.jaxb.Marshaller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBException;

/**
 *
 * @author EGHOS
 */
@WebServlet(name = "FrontController", urlPatterns = {Patterns.FC})
public class FrontController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */ 
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/xml;charset=UTF-8");    
        String  action  = request.getParameter(Attributes.ACTION);
        
        
        if(Actions.VIEW_PRODUCTS.equals(action)){
            ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute(Attributes.CART);
            if(cart == null)
                request.getSession().setAttribute(Attributes.CART, new ShoppingCart());
            try {
                Marshaller.marshal(new CarList(Database.getProducts()), response.getWriter());
            } catch (JAXBException ex) {
                request.getSession().setAttribute(Attributes.MSG, ex.getLocalizedMessage());
                response.sendRedirect(Attributes.MSG);
            }
        }
        else if(Actions.PAY.equals(action)){
            ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute(Attributes.CART);
            if(cart == null || cart.getTotal() == null){
                request.getRequestDispatcher(Pages.HOME).forward(request, response);
                return;
            }
            cart.calculateTotal();
            try {
                Marshaller.marshal(cart, response.getWriter());
            } catch (JAXBException ex) {
                request.getSession().setAttribute(Attributes.MSG, ex.getLocalizedMessage());
                response.sendRedirect(Attributes.MSG);
            }
        }
        else if(Actions.CHECK_LOGIN.equals(action)){
            response.setContentType("text/plain;charset=UTF-8");
            Customer c = (Customer) request.getSession().getAttribute(Attributes.USER);
            if(c == null){
                response.getWriter().write("false");            
            }else
                response.getWriter().write(c.getLogin()); 
        }
        else if(Actions.CONTACT_US.equals(action)){
            
        }
        else if(Actions.GET_HEADER.equals(action)){
            request.getRequestDispatcher(Pages.HEADER).forward(request, response);
        }
        else if(Actions.GET_FOOTER.equals(action)){
            request.getRequestDispatcher(Pages.FOOTER).forward(request, response);
        }
        else if(Actions.LOGIN.equals(action)){
            request.getRequestDispatcher(Requests.LOGIN).forward(request, response);
        }
        else if(Actions.LOGOUT.equals(action)){
            request.getRequestDispatcher(Requests.LOGOUT).forward(request, response);
        }
        else if(Actions.REGISTER.equals(action)){
            request.getRequestDispatcher(Requests.REGISTER).forward(request, response);
        }
        else if(Actions.VIEW_CART.equals(action)){
            ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute(Attributes.CART);
            cart.calculateTotal();
            try {
                Marshaller.marshal(cart, response.getWriter());
            } catch (JAXBException ex) {
                request.getSession().setAttribute(Attributes.MSG, ex.getLocalizedMessage());
                response.sendRedirect(Attributes.MSG);
            }
        }
        else if(Actions.ADD_TO_CART.equals(action)){
            String id = request.getParameter(Attributes.PRODUCT_ID);
            ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute(Attributes.CART);
            cart.addItem(Database.getProduct(Long.parseLong(id)));
            request.getRequestDispatcher(Pages.HOME).forward(request, response);
            
        }
        else if(Actions.REMOVE_FROM_CART.equals(action)){
            String id = request.getParameter(Attributes.PRODUCT_ID);
            ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute(Attributes.CART);
            cart.removeItem(Long.parseLong(id));
            request.getRequestDispatcher(Pages.VIEW_CART).forward(request, response);
        }
        else if(Actions.EMPTY_CART.equals(action)){
            ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute(Attributes.CART);
            cart.emptyCart();
            request.getRequestDispatcher(Pages.VIEW_CART).forward(request, response);
        }else if(Actions.MAIL.endsWith(action)){
            request.getRequestDispatcher(Requests.MAIL).forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
